﻿using HW;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace HTTPClient
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();
        public static readonly JsonSerializerOptions op = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            PropertyNameCaseInsensitive = true

        };
        private static JsonSerializerOptions _options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

        static readonly string miyao="pV4q4Sa07NB3wDA89Wun3UewGrqTpE836";
        static async Task Main(string[] args)
        {
            /*var repositories = await ProcessRepositories();

            foreach (var repo in repositories)
            {
                Console.WriteLine(repo.Name);
                Console.WriteLine(repo.Description);
                Console.WriteLine(repo.GitHubHomeUrl);
                Console.WriteLine(repo.Homepage);
                Console.WriteLine(repo.Watchers);
                Console.WriteLine(repo.LastPush);
                Console.WriteLine();
            }*/

            await HttpPostAsync();
            //var s = await GetLocation();

            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
var m = random.Next(3);
            Console.WriteLine(m);
            }
            
        }

        private static async Task<string> GetLocation()
        {
            client.DefaultRequestHeaders.Accept.Clear();

            //发送get请求
            var str = await client.GetStringAsync("http://api.cellocation.com:83/cell/?output=json&coord=gcj02&mcc=460&mnc=0&lac=12858&ci=132141134");
            Console.WriteLine(str);
            return str;
        }

        /// <summary>
        /// HTTP GET 请求 测试
        /// </summary>
        /// <returns></returns>
        private static async Task<List<Repository>> ProcessRepositories()
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
            client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

            var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos");
            var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);
            return repositories;
        }

        /// <summary>
        /// HTTP POST请求
        /// </summary>
        /// <returns></returns>
        private static async Task HttpPostAsync()
        {
            
            //client.DefaultRequestHeaders.Add("timesnmp", "111222");
            client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

            //string user = JsonSerializer.Serialize(new User(), _options);

            //雨量
            string data = JsonSerializer.Serialize(new Rainfall(), _options);
            //水位
            string shuiwei = JsonSerializer.Serialize(new WaterStage(), _options);
           

            //var content = new StringContent(data,Encoding.UTF8,"application/json");
            var content = new StringContent(shuiwei,Encoding.UTF8,"application/json");
            //content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //var md = data + miyao;
            var md = shuiwei + miyao;
            content.Headers.Add("X-Sign", md?.MD5());
            content.Headers.Add("X-TIMESTAMP", GetTimeStamp().ToString());

            //var response = await client.PostAsync("http://shuiku.sdjyxxkj.top/sdjy-server/receiver/safetymdata", content);
            var response = await client.PostAsync("http://reservoir.hbwpd.com/sdjy-server/receiver/rtudata", content);
            //var response = await client.PostAsync("https://api.apiopen.top/api/login", content);
            //response.EnsureSuccessStatusCode();
            var t = await response.Content.ReadAsStringAsync();
            Console.WriteLine(t);
            //Console.WriteLine(data);
            Console.WriteLine(shuiwei);
        }

        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp()
        {
            var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            //TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            //var timestamp = Convert.ToInt64(ts.TotalMilliseconds);
            return timestamp;
        }

        

    }

    /// <summary>
    /// 渗流量
    /// </summary>
    public class Seepage
    {
        /// <summary>
        /// 水库注册登记号
        /// </summary>
        [JsonPropertyName("rscd")]
        public string Rscd { get; set; } = "13080350001-A5";
        /// <summary>
        /// 水工建筑物编号
        /// </summary>
        [JsonPropertyName("hycncd")]
        public string Hycncd { get; set; } = "101";
        /// <summary>
        /// 测点编号
        /// </summary>
        [JsonPropertyName("mpcd")]
        public string Mpcd { get; set; }="10101101";
        /// <summary>
        /// 测量时间
        /// </summary>
        [JsonPropertyName("mstm")]
        public string Mstm { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// 渗流量
        /// </summary>
        [JsonPropertyName("spprwl")]
        public int Spprwl { get; set; } = 1;
        /// <summary>
        /// 数据来源
        /// </summary>
        [JsonPropertyName("source")]
        public string Source { get; set; } = "rtu";
        /// <summary>
        /// sp
        /// </summary>
        [JsonPropertyName("type")]
        public string Type { get; set; } = "SP";
    }

    /// <summary>
    /// 雨量
    /// </summary>
    public class Rainfall
    {
        /// <summary>
        /// 测站编码
        /// </summary>
        public string STCD { get; set; } = "301H600W";
        /// <summary>
        /// 测站名称
        /// </summary>
        public string RTUNM { get; set; } = "湛营水库";
        /// <summary>
        /// PP
        /// </summary>
        public string TYPE { get; set; } = "PP";
        /// <summary>
        /// 数据时间
        /// </summary>
        public string YMDHM { get; set; } = DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// 数据到达时间
        /// </summary>
        public string DOWNDATE { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// 信道类型
        /// </summary>
        public string NFOINDEX { get; set; } = "";
        /// <summary>
        /// 数据类型
        /// </summary>
        public string DATATYPE { get; set; } = "C";
        /// <summary>
        /// 天气情况,7,8,9,雨，阴，晴
        /// </summary>
        public string WTH { get; set; } = "9";
        /// <summary>
        /// 累计值
        /// </summary>
        public double DATA { get; set; }
        /// <summary>
        /// 降雨量
        /// </summary>
        public string DTRN { get; set; } = "0.08";
        /// <summary>
        /// 设备唯一标识
        /// </summary>
        public string SOURCE { get; set; } = "xbczysh";

    }

    /// <summary>
    /// 水位
    /// </summary>
    public class WaterStage
    {
        /// <summary>
        /// 测站编码
        /// </summary>
        public string STCD { get; set; } = "301H600W";
        /// <summary>
        /// 测站名称
        /// </summary>
        public string RTUNM { get; set; } = "湛营水库";
        /// <summary>
        /// PP
        /// </summary>
        public string TYPE { get; set; } = "ZZ";
        /// <summary>
        /// 数据时间
        /// </summary>
        public string YMDHM { get; set; } = DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// 数据到达时间
        /// </summary>
        public string DOWNDATE { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        /// <summary>
        /// 信道类型
        /// </summary>
        public string NFOINDEX { get; set; } = "";
        /// <summary>
        /// 数据类型
        /// </summary>
        public string DATATYPE { get; set; } = "F";
        /// <summary>
        /// 水势，4，5，6，落，涨，平
        /// </summary>
        public string ZRTEND { get; set; } = "6";
        /// <summary>
        /// 原始水位
        /// </summary>
        public double DATA { get; set; } = 0;
        /// <summary>
        /// 水位
        /// </summary>
        public double ZR { get; set; } = 520.20;
        /// <summary>
        /// 流量
        /// </summary>
        public double Q { get; set; } = 0;
        /// <summary>
        /// 设备唯一标识
        /// </summary>
        public string SOURCE { get; set; } = "xbczysh";
    }

    public class User
    {
        [JsonPropertyName("account")]
        public string Account { get; set; } = "1197066591@qq.com";
        [JsonPropertyName("password")]
        public string Password { get; set; } = "123456";
    }
    public class Repository
    {
        [JsonPropertyName("name")]
        public string Name { get; set; }

        [JsonPropertyName("description")]
        public string Description { get; set; }

        [JsonPropertyName("html_url")]
        public Uri GitHubHomeUrl { get; set; }

        [JsonPropertyName("homepage")]
        public Uri Homepage { get; set; }

        [JsonPropertyName("watchers")]
        public int Watchers { get; set; }

        [JsonPropertyName("pushed_at")]
        public string JsonDate { get; set; }

        public DateTime LastPush =>
            DateTime.ParseExact(JsonDate, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
    }
}
